﻿using System;
using System.IO;

namespace CopyOfTeacher
{
    public static class CopyDir
    {
        //static void Main(string[] args)
        //{
        //    bool copy = CopyDirectory("c:\\temp\\index\\", "c:\\temp\\newindex\\", true);
        //}

        public static  bool CopyDirectory(string src, string des, bool overwrite)
        {
            bool ret;
            try
            {
                src = src.EndsWith(@"\") ? src : src + @"\";
                des = des.EndsWith(@"\") ? des : des + @"\";

                if (Directory.Exists(src))
                {
                    if (!Directory.Exists(des)) Directory.CreateDirectory(des);

                    foreach (string fls in Directory.GetFiles(src))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(des + flinfo.Name, overwrite);
                    }
                    foreach (string drs in Directory.GetDirectories(src))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (!CopyDirectory(drs, des + drinfo.Name, overwrite))
                            ret = false;
                    }
                }
                ret = true;
            }
            catch (Exception)
            {
                ret = false;
            }

            return ret;
        }
    }
}
